package boundary;

import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Choice;

import client.Client;
import entity.CasualSubscription;
import entity.Customer;
import entity.FullSubscription;
import entity.ParkingLot;
import entity.Prices;
import entity.Subscription;
import entity.Subscription.subOrderEnum;
import controller.SubscriptionOrder;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;
import javax.swing.JCheckBox;
import java.awt.Font;
import javax.swing.AbstractAction;
import javax.swing.Action;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JList;
import javax.swing.JFormattedTextField;
import java.awt.Choice;
import java.sql.Time;

public class SubscriptionOrderGui extends JPanel{
	private JPanel lastPanel;
	private JFrame frame;
	private boolean check1=false;
	private JTextField textField_1;
	private Subscription sub;
	private final Action action = new SwingAction();
	private JTextField textField;
	
	public void setLastPanel (JPanel panel)
	{
		this.lastPanel=panel;
	}
	public void setFrame (JFrame frame)
	{
		this.frame=frame;
	}
	
	public SubscriptionOrderGui() {
		super();
		
		//setTitle("Subscription Order");
		this.setLayout(null);
		
		JLabel lblCarNumber = new JLabel("car number");
		lblCarNumber.setBounds(10, 52, 71, 20);
		this.add(lblCarNumber);
		
		textField_1 = new JTextField();
		textField_1.setBounds(119, 52, 216, 20);
		this.add(textField_1);
		textField_1.setColumns(10);
		
		final Choice choice = new Choice();
		choice.setBounds(119, 106, 111, 20);
		ParkingLot [] plots=Client.getParkingLots_sorted();
		for (int i=0;i<6; i++)
			choice.add(plots[i].getParking_lot_name());
		this.add(choice);
		
		final Choice choice_1 = new Choice();
		choice_1.setBounds(119, 78, 47, 20);
		for(int i=0; i<24;i++)
			choice_1.add(String.valueOf(i));
		this.add(choice_1);
		
		final Choice choice_2 = new Choice();
		choice_2.setBounds(172, 78, 34, 22);
		choice_2.add("00");
		choice_2.add("15");
		choice_2.add("30");
		choice_2.add("45");
		this.add(choice_2);
		
		JButton btnSend = new JButton("send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (check1)
					sub=new FullSubscription();
				else
				{
					sub=new CasualSubscription();
					/*String leaveTime=txtAs.getText();
					int hh=(leaveTime.charAt(0)-'0')*10 + leaveTime.charAt(1);
					int mm=(leaveTime.charAt(3)-'0')*10 + leaveTime.charAt(4);
					int ss=(leaveTime.charAt(6)-'0')*10 + leaveTime.charAt(7);
				*/	((CasualSubscription) sub).setleaving_time(new Time(Integer.valueOf(choice_1.getSelectedItem()),Integer.valueOf(choice_2.getSelectedItem()),0));
					int plNum;
					for (int i=0;i<6;i++)
						if (choice.getItem(i)==choice.getSelectedItem())
							((CasualSubscription)sub).setParking_lot_number(i+1);
				}
				sub.setCar_number(textField_1.getText());
				sub.setCustomer_id(Client.getLoggedIn());
				subOrderEnum validation=sub.validate();
				switch (validation)
				{
				case CORRECT:
					sub.save();
					final Prices p=Client.getCurrentPrices(1);
					Customer customers[]=Client.getCustomers();
					Customer customer = null;
					for (int i=0; i<customers.length; i++)
						if (customers[i].getCostumer_id()==Client.getLoggedIn())
							customer=customers[i];
					if (check1)
						customer.AddBalance(-p.getFullMonthlySubscriber());
					else
						customer.AddBalance(-p.getMonthlyRegularSubscriber());
					JOptionPane.showMessageDialog(null,"Your subscription is now activated for 28 days\nSubscription number:  "+sub.getSubscription_number(),"Subscription Order",JOptionPane.INFORMATION_MESSAGE);
					break;
				case ID_ERROR:
					JOptionPane.showMessageDialog(null,"Wrong ID number","Subscription Order",JOptionPane.ERROR_MESSAGE);
					break;
				case CAR_NUMBER_ERROR:
					JOptionPane.showMessageDialog(null,"Wrong car number","Subscription Order",JOptionPane.ERROR_MESSAGE);
					break;
				}
			}
		});
		btnSend.setBounds(68, 201, 89, 23);
		this.add(btnSend);
		
		
		
		
		JButton btnCancel = new JButton("cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				frame.setContentPane((JPanel)lastPanel);
				//JOptionPane.showMessageDialog(null,String.valueOf(choice.getItem(0)),"Subscription Order",JOptionPane.ERROR_MESSAGE);
			}
		});
		btnCancel.setBounds(246, 201, 89, 23);
		this.add(btnCancel);
		
		
		
		
		JButton button = new JButton("?");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				JOptionPane.showMessageDialog(null,"Choose Full Subscription for free entrance to any parking lot","Full Subscription",JOptionPane.INFORMATION_MESSAGE);
			}
		});
		button.setFont(new Font("Tahoma", Font.PLAIN, 18));
		button.setBounds(10, 141, 47, 24);
		this.add(button);
		
		JCheckBox chckbxFullSubscription = new JCheckBox("Full Subscription");
		chckbxFullSubscription.setToolTipText("");
		chckbxFullSubscription.addMouseListener(new MouseAdapter() 
		{
			public void mouseClicked(MouseEvent arg0) {
				if (check1)
					check1=false;
				else
					check1=true;
				choice.setEnabled(!check1);
				choice_1.setEnabled(!check1);
				choice_2.setEnabled(!check1);
				
			}
		});
		chckbxFullSubscription.setBounds(68, 145, 111, 23);
		this.add(chckbxFullSubscription);
		
		JLabel lblLeavingHour = new JLabel("leaving hour");
		lblLeavingHour.setBounds(11, 83, 70, 14);
		this.add(lblLeavingHour);
		
		JLabel lblId = new JLabel("ID");
		lblId.setBounds(10, 27, 46, 14);
		this.add(lblId);
		
		textField = new JTextField();
		textField.setBounds(119, 24, 216, 20);
		this.add(textField);
		textField.setColumns(10);
		
		
		JLabel lblParkingLot = new JLabel("parking lot");
		lblParkingLot.setBounds(10, 108, 71, 14);
		this.add(lblParkingLot);
		
		
	}
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "SwingAction");
			putValue(SHORT_DESCRIPTION, "Some short description");
		}
		public void actionPerformed(ActionEvent e) {
		}
	}
}
